function EventEmiter() {

}
EventEmiter.prototype.on = function(key ,cb) {
  if(!this.events) {
    this.events = Object.create(null)
  }
  this.events[key] = this.events[key] ? this.events[key] : []
  this.events[key].push(cb)
  if(key !== 'newListener') {
    this.emit('newListener', key)
  }
}

EventEmiter.prototype.once = function(key ,cb) {
  // 这样 会有bug  当我要立即删除的时候  就不是原来的 函数了   用户自己删不了
  // function one() {
  //   cb()
  //   this.off(key, one)
  // }

  cb.l = cb
  this.on(key, cb)
  
}

EventEmiter.prototype.off = function(key, cb) {
  if(this.events[key]) {
    this.events[key] = this.events[key].filter(fn => fn!==cb)
  }
}

EventEmiter.prototype.emit = function(key, ...args) {
  if(this.events[key]) {
    this.events[key].forEach(fn => {
      fn.call(this, ...args)
      if(fn.l) {
        this.off(key, fn.l)
      }
    })
  }
}

module.exports = EventEmiter